﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<MvcCmsForum.Models.ViewData.ForumModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <%=ViewData["PageTitle"]%>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
    <style type="text/css">
        label.error
        {
            float: none;
            color: red;
            padding-left: .5em;
            vertical-align: top;
        }
    </style>
    <%if (bool.Parse(ViewData["NoIndex"].ToString()))
      {%>
    <meta name="ROBOTS" content="NOINDEX, FOLLOW">
    <%}%>
    <link href="/Content/start/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    <link href="/content/app_themes/<%=ViewData["SkinName"] %>/css/Forum.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
        <div class="spacer_top">
        </div>
        <div id="thecontent" class="thecontent">
            <table width="100%"><tr>
            <td>
            <strong>
                <a href="/forums/<%=Html.Encode(Model._Forum.Path)%>/">
                    <%=Html.Encode(Model._Forum.Title) %>
                    Forum</a></strong>
            </td>
            <td align="right">
            </td>
            </tr></table>
            <h3>
                <%if (Model._Post.ThreadParentPost != null)
                  { %>
                <a href="/forums/forum-<%=Html.Encode(Model._Post.ThreadParentPost.Forum.Path) %>/<%=Html.Encode(Model._Post.ThreadParentPost.Path) %>">
                    <%=Html.Encode(Model._Post.ThreadParentPost.Title)%></a>
                <%} %><br />
                <%=Html.Encode(Model._Post.Title) %>
            </h3>
            <span class="field-validation-error">&nbsp;<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
                <%=ViewData["message"].ToString()%>
            </span>
            <% 
                var userVote = int.Parse(ViewData["userVote"].ToString()); 
            %>
            <div id="posts">
                <div id="post-<%= Model._Post.PostID %>" class="post">
                    <div id="editPostContent">
                        <% if ((User.Identity.Name == Model._Post.ThreadUser.UserName) ||
                                       (User.IsInRole("Editor") || User.IsInRole("Admin")
                                       ))
                           { %>
                        <a href="javascript:void(0);" onclick="EditPost(<%= Model._Post.PostID %>, false)">edit</a>
                        <% } %>
                    </div>
                    <div class="vote-button">
                        <a onclick="vote(<%= Model._Post.PostID %>, 'up')" class="vote-up<%= userVote >= 1 ? " selected" : "" %>"
                            href="javascript:void(0);">Like</a> <strong>
                                <%= Model._Post.VoteCount%></strong> <a class="vote-down<%= userVote <= -1 ? " selected" : "" %>"
                                    onclick="vote(<%= Model._Post.PostID %>, 'down')" href="javascript:void(0);">Dislike</a>
                    </div>
                    <div id="originalPostContent" class="postbody">
                        <%= Model._Post.Body.ToString()%></div>
                    <div id="previewPostContent" class="postbody" style="display: none;">
                    </div>
                    <div class="posted-last">
                        <span class="posted-at">
                            <%= Model._Post.AddedDate.ToUtcTimeSinceString()%>
                            ago</span> <span class="posted-by">by
                                <img src="<%= Model._Post.AddedByAvatarUrl %>" />
                                <%= Model._Post.ThreadUser.UserName.ToProfileLink()%></span>
                    </div>
                </div>
                <hr style="visibility: hidden;" />
                <h3>
                    Replies</h3>
                <div id="forum-post-replies">
                    <% foreach (var post in Model._Posts)
                       { %>
                    <div class="reply-container">
                        <a id="pst-<%= post.PostID %>" href="#"></a>
                        <div id="reply-<%= post.PostID %>" class="reply">
                            <div id="editPostContent<%= post.PostID %>">
                            </div>
                            <div class="vote-button">
                                <a onclick="vote(<%= post.PostID %>, 'up')" class="vote-up" href="javascript:void(0);">
                                    Like</a> <strong>
                                        <%= post.VoteCount%></strong> <a class="vote-down" onclick="vote(<%= post.PostID %>, 'down')"
                                            href="javascript:void(0);">Dislike</a>
                            </div>
                            <div class="postbody">
                                <div class="reply-header">
                                    Reply posted by <span class="name">
                                        <img src="<%= post.AddedByAvatarUrl %>" />
                                        <%= post.ThreadUser.UserName.ToProfileLink()%></span>
                                    <%= post.LastPostDate.ToUtcTimeSinceString()%>
                                    ago <a href="/forums/forum-<%=Html.Encode(post.Forum.Path) %>/<%=Html.Encode(post.Path) %>">
                                        View Branch And Reply</a>
                                    <%
                                        if (!post.ThreadParentPost.IsAnswered && (User.IsInRole("Moderator") ||
                                              User.Identity.Name == post.ThreadUser.UserName))
                                        {%>
                                    <a id="mrkanswer-<%= post.PostID%>" href="javascript:void(0);" onclick="MarkAnswer(<%= post.PostID%>)">
                                        Mark As Answer</a>
                                    <%}
                                        if (post.IsAnswer)
                                        {
                                    %>
                                    <span style="color: Red">Answer</span>
                                    <%}%>
                                </div>
                                <div class="admin">
                                    <% if (User.IsInRole("Editor") || User.IsInRole("Admin"))
                                       { %>
                                    <div>
                                        <a class="remove" meta:id="<%= post.PostID %>" href="#remove">remove</a></div>
                                    <% } %>
                                    <% if ((User.Identity.Name == post.ThreadUser.UserName) ||
                                       (User.IsInRole("Editor") || User.IsInRole("Admin")
                                       ))
                                       { %><div>
                                           <a href="javascript:void(0);" onclick="EditPost(<%= post.PostID %>, true)">edit</a></div>
                                    <% } %>
                                </div>
                                <div id="originalPostContent<%= post.PostID %>">
                                    <blockquote class="body">
                                        <%= post.Body %></blockquote>
                                </div>
                                <div id="previewPostContent<%= post.PostID %>" class="postbody" style="display: none;">
                                </div>
                                <div>
                                    <%if (post.Replies.Count() > 0)
                                      {%>
                                    Replies:
                                    <%} %>
                                    <%foreach (var reply in post.Replies)
                                      {
                                          int stubLenght = 100;
                                          if (reply.Body.Length < 99) { stubLenght = reply.Body.Length - 1; }
                                           
                                    %>
                                    <div class="reply-reply">
                                        <blockquote class="body">
                                            <%= reply.Body.Substring(0, stubLenght)%>...</blockquote>
                                        by
                                        <%=reply.ReplyUser.UserName.ToProfileLink()%>
                                    </div>
                                    <%} %>
                                </div>
                            </div>
                        </div>
                    </div>
                    <% } %>
                </div>
                <% 
                    var paginationReply = Model._Posts.FirstOrDefault();
                    if (paginationReply != null)
                    {
                        Html.RenderPartial("ControlsForum/Pager", paginationReply); 
                    }
                %>
            </div>
            <% if (!Model._Post.Closed)
               { %>
            <form method="post" action="/forums/posts/<%= Model._Post.PostID %>/reply" class="post-reply-create">
            <input type="hidden" id="postId" name="postId" value="<%= Model._Post.PostID %>" />
            <input type="hidden" id="title" name="title" value="RE: <%= Model._Post.Title %>" />
            <p class="field input">
                <label for="body">
                    Enter Your Reply -
                    <%=Html.ActionLink("Forum Code Is On", "FAQ", "MvcCmsForum", null,  new { target = "_blank" })%>
                    - Html is <strong>Off</strong></label><br />
                <%= Html.TextArea("body", String.Empty, new { @class = "replyTA" })%>
                <span class="input-message"></span>
            </p>
            <hr />
            <p>
                <button type="submit" id="post-reply-create-button">
                    Add Reply</button></p>
            </form>
            <% }
               else
               { %>
            <div class="info">
                This post has been closed, and no more replies can be made.</div>
            <% } %>
        </div>
    </div>
    <div id="editPostForm">
        <form action="/forums-editpost" method="post">
        <fieldset>
            <label for="Source">
                Source:
                <%=Html.ActionLink("Forum Code Is On", "FAQ", "MvcCmsForum", null,  new { target = "_blank" })%></label>
            <%= Html.TextArea("Source", new { @class = "editpost-ta" })%>
            <%= Html.Hidden("EditedPostID") %>
            <%= Html.Hidden("IsReply") %>
            <%= Html.Hidden("PageNo", ViewData["index"]) %>
            <input type="submit" value="Save" />
            <input id="cancelEdit" type="button" value="Cancel" />
        </fieldset>
        </form>
    </div>
</asp:Content>
<asp:Content ID="SidebarContent" ContentPlaceHolderID="SidebarContent" runat="server">
    <% Html.RenderPartial("ControlsForum/AdminSidebar"); %>
</asp:Content>
<asp:Content ID="ScriptContent" ContentPlaceHolderID="ScriptContent" runat="server">

    <script src="/Scripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        LoadRecentPosts();
        var original, preview, cnt;
        var currentPostId;
        var currentIsReply;
        cnt = $('#editPostContent');
        original = $('#originalPostContent');
        preview = $('#previewPostContent');
        function EditPost(postid, isReply) {
            var dlg = $('#editPostForm');
            if (isReply) {
                cnt = $('#editPostContent' + postid);
                original = $('#originalPostContent' + postid);
                preview = $('#previewPostContent' + postid);
                currentIsReply = true;
            }
            else {
                cnt = $('#editPostContent');
                original = $('#originalPostContent');
                preview = $('#previewPostContent');
                currentIsReply = false;
            }
            $('#IsReply').val(currentIsReply);
            currentPostId = postid;
            $('#EditedPostID').val(postid);
            if (!dlg.dialog('isOpen')) {
                $.post('/forums/' + postid + '/source/', function(data) {
                    $('#Source').val(data);
                    original.hide();
                    preview.html(original.html()).show();
                    dlg.dialog('open');
                });
            } else {
                dlg.dialog('close');
            }
        }

        var timeout = null;
        $(function() {

            var dlg = $('#editPostForm');
            var cntPos = cnt.position();
            dlg.dialog({ autoOpen: false,
                width: 550,
                position: [cntPos.left - 300 + cnt.outerWidth(), cntPos.top + cnt.outerHeight() + 150],
                show: 'blind',
                hide: 'blind',
                resizable: true,
                beforeclose: function() { original.show(); preview.hide(); }
            });

            $('#Source').keyup(function(e) {
                if (timeout != null) {
                    clearTimeout(timeout);
                    timeout = null;
                }

                var self = $(this);
                timeout = setTimeout(function() {
                    $.post('/forums-editpost/' + currentPostId + '/GetPostPreview',
                           { source: self.val() },
                           function(data) { preview.html(data); });
                }, 250);
            });

            $('#cancelEdit').click(function() {
                dlg.dialog('close');
            });
        });    
    
    
    </script>

    <% if (User.IsInRole("Editor"))
       { %>

    <script type="text/javascript" src="/Areas/MvcCmsForum/scripts/manage-forums.js"></script>

    <% } %>

    <script type="text/javascript" src="/Areas/MvcCmsForum/scripts/forums.js"></script>

</asp:Content>
